package com.example.administrator.shandon_3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.nle.mylibrary.forUse.zigbee.Zigbee;
import com.nle.mylibrary.forUse.zigbee.ZigbeeControlListener;
import com.nle.mylibrary.transfer.DataBusFactory;

public class MainActivity extends AppCompatActivity {
    private TextView tv_temp, tv_light;
    private EditText et_maxtemp, et_mintemp, et_minlight, et_maxlight;
    private ToggleButton btn_open;
    private Zigbee zigbee;
    private Double maxtemp,mintemp,minlight,maxtlight;
    private static  Mythread mythread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btn_open.isChecked()) {
                    mythread =new Mythread();
                    mythread.start();
                } else {
                    mythread.interrupt();
                    tv_temp.setText("");
                    tv_light.setText("");
                    try {
                        zigbee.ctrlDoubleRelay(1, 2, false, new ZigbeeControlListener() {
                            @Override
                            public void onCtrl(boolean isSuccess) {
                                Log.i("main",isSuccess+"");
                            }
                        });
                        zigbee.ctrlDoubleRelay(1, 1, false, new ZigbeeControlListener() {
                            @Override
                            public void onCtrl(boolean isSuccess) {
                                Log.i("main",isSuccess+"");
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void init() {
        tv_temp = findViewById(R.id.tv_temp);
        tv_light = findViewById(R.id.tv_light);
        et_maxtemp = findViewById(R.id.tv_maxtemp);
        et_mintemp = findViewById(R.id.tv_mintemp);
        et_maxlight = findViewById(R.id.tv_maxlight);
        et_minlight = findViewById(R.id.tv_minlight);
        btn_open = findViewById(R.id.btn_open);
        zigbee=new Zigbee(DataBusFactory.newSocketDataBus("172.21.2.200",4196));
    }

    private class Mythread extends Thread
    {
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                try {
                    double light = zigbee.getLight();
                    double[] tmpHum = zigbee.getTmpHum();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_light.setText(light+"Lx");
                            tv_temp.setText(tmpHum[0]+"℃");
                        }
                    });
                    if(et_maxlight.getText().toString()!=null&&et_maxtemp.getText().toString()!=null&&et_minlight.getText().toString()!=null&&et_mintemp.getText().toString()!=null){
                        minlight= Double.valueOf(et_minlight.getText().toString());
                        maxtemp= Double.valueOf(et_maxtemp.getText().toString());
                        mintemp= Double.valueOf(et_mintemp.getText().toString());
                        maxtlight= Double.valueOf(et_maxlight.getText().toString());
                        if(light<Double.parseDouble(et_minlight.getText().toString())){
                            zigbee.ctrlDoubleRelay(1, 1, true, new ZigbeeControlListener() {
                                @Override
                                public void onCtrl(boolean isSuccess) {
                                    Log.i("main",isSuccess+"");
                                }
                            });
                        }else if(light>maxtlight){
                            zigbee.ctrlDoubleRelay(1, 1, false, new ZigbeeControlListener() {
                                @Override
                                public void onCtrl(boolean isSuccess) {
                                    Log.i("main",isSuccess+"");
                                }
                            });
                        }
                        if(tmpHum[0]>maxtemp){
                            zigbee.ctrlDoubleRelay(1, 2, true, new ZigbeeControlListener() {
                                @Override
                                public void onCtrl(boolean isSuccess) {
                                    Log.i("main",isSuccess+"");
                                }
                            });
                        }else if(tmpHum[0]<mintemp){
                            zigbee.ctrlDoubleRelay(1, 2, false, new ZigbeeControlListener() {
                                @Override
                                public void onCtrl(boolean isSuccess) {
                                    Log.i("main",isSuccess+"");
                                }
                            });
                        }
                    }else {
                        Toast.makeText(MainActivity.this,"请把规则临界点填写完整,临界点只支持正整数",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
